﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RAMED
{
    public partial class ModifierCorrectionForm : Form
    {
      



      public CORRECTION corre
      {
          get;
          set;
      }

      public List<Type_Correction> TypesAjoutes = new List<Type_Correction>();
      public List<Type_Correction> TypesSelectionnes = new List<Type_Correction>();
      public List<Type_Correction> TypesAjoutes0 = new List<Type_Correction>();
      public List<Type_Correction> TypesSelectionnes0 = new List<Type_Correction>();

      public Recherche_correction recherchecorrection { get; set; }


        public ModifierCorrectionForm()
        {
            InitializeComponent();
            RAMEDEntities1 context = SingletonContext.GetContext();
            comboBoxannexe.DataSource = context.Annexe_administratif;
            comboBoxannexe.DisplayMember = "nom_annexe_administratif";
            comboBoxannexe.ValueMember = "id_annexe_administratif";
            comboBoxresponsable.DataSource = context.Responsables;
            comboBoxresponsable.DisplayMember = "nom_prenom_responsable";
            comboBoxresponsable.ValueMember = "id_responsable";
            
        }

        public void InitialiserChamps()
        {
            txtnom.Text = corre.Nom;
            txtprenom.Text = corre.Prénom;
            txtcin.Text = corre.Cin;
            txtnumero.Text = corre.Num_dossier.ToString();
            comboBoxannexe.SelectedValue = corre.id_annexe_administratif;
            comboBoxresponsable.SelectedValue = corre.id_Responsable;


            foreach (Association_Correction_Type act in corre.Association_Correction_Type)
            {
                TypesAjoutes.Add(act.Type_Correction);
            }
            listBox4.DisplayMember = "description";
            listBox4.DataSource = TypesAjoutes;
          

            RAMEDEntities1 context = SingletonContext.GetContext();
            TypesSelectionnes = context.Type_Correction.ToList();
            foreach (Association_Correction_Type act in corre.Association_Correction_Type)
            {

                if (TypesSelectionnes.Exists(ts => ts.id_Type_Correction == act.id_Type_Correction))
                    TypesSelectionnes.RemoveAll(ts => ts.id_Type_Correction == act.id_Type_Correction);
            }

            listBox3.DisplayMember = "description";
            listBox3.DataSource = TypesSelectionnes;


            TypesAjoutes0.AddRange( TypesAjoutes);
            //TypesSelectionnes0 = TypesSelectionnes;
        }

        private void ModifierCorrectionForm_Load(object sender, EventArgs e)
        {

        }

        private void btnenregistrer_Click(object sender, EventArgs e)
        {
            RAMEDEntities1 context = SingletonContext.GetContext();
            corre.Nom = txtnom.Text;
            corre.Prénom = txtprenom.Text;
            corre.Cin = txtcin.Text;
            corre.Num_dossier = utitlitaire.CasterString(txtnumero.Text);
            Annexe_administratif annexe_administratif = ((Annexe_administratif)comboBoxannexe.SelectedItem);
            corre.id_annexe_administratif = annexe_administratif.id_annexe_administratif;
            Responsable  Responsable = ((Responsable)comboBoxresponsable.SelectedItem);
            corre.id_Responsable = Responsable.id_responsable;
            recherchecorrection.mettreAjouterFormGridView();
            context.SaveChanges();
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void Button21_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void comboBoxresponsable_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btn1_Click(object sender, EventArgs e)
        {
            List<Type_Correction> type_corrections = new List<Type_Correction>();
            type_corrections = listBox3.SelectedItems.Cast<Type_Correction>().ToList();



            foreach (Type_Correction tp in type_corrections)
                TypesSelectionnes.Remove(tp);
            listBox3.DataSource = null;
            listBox3.DataSource = TypesSelectionnes;
            listBox3.DisplayMember = "description";
            listBox3.Refresh();

            TypesAjoutes.AddRange(type_corrections);

            listBox4.DataSource = null;
            listBox4.DataSource = TypesAjoutes;
            listBox4.DisplayMember = "description";
            listBox4.Refresh();
        }

        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btn2_Click(object sender, EventArgs e)
        {
            List<Type_Correction> type_corrections = new List<Type_Correction>();
            type_corrections = listBox4.SelectedItems.Cast<Type_Correction>().ToList();


            foreach (Type_Correction tp in type_corrections)
                TypesAjoutes.Remove(tp);
            listBox4.DataSource = null;
            listBox4.DataSource = TypesAjoutes;
            listBox4.DisplayMember = "description";
            listBox4.Refresh();

            TypesSelectionnes.AddRange(type_corrections);

            listBox3.DataSource = null;
            listBox3.DataSource = TypesSelectionnes;
            listBox3.DisplayMember = "description";
            listBox3.Refresh();
        }



        private void MettreAjourTypesCorrection(CORRECTION corre)
        {
            foreach (Type_Correction tca in TypesAjoutes)
            {
                if (!TypesAjoutes0.Exists(ta0 => ta0.id_Type_Correction == tca.id_Type_Correction))
                {
                    Association_Correction_Type ast = new Association_Correction_Type();
                    ast.id_Type_Correction = tca.id_Type_Correction;
                    corre.Association_Correction_Type.Add(ast);
                }
            }

            foreach (Type_Correction tca0 in TypesAjoutes0)
            {
                if (!TypesAjoutes.Exists(tca => tca.id_Type_Correction == tca0.id_Type_Correction))
                {
                      RAMEDEntities1 context = SingletonContext.GetContext();
                      corre.Association_Correction_Type.Remove(corre.Association_Correction_Type.First(a => a.id_Type_Correction == tca0.id_Type_Correction));

                }
            }
        }
    }
}
